﻿// -----------------------------------------------------------------------
// <copyright file="ProfileTypeGroupData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db ProfileTypeGroup
    /// </summary>
    public class ProfileTypeGroupData
    {
        /// <summary>
        /// Get ProfileTypeGroup By Id
        /// </summary>
        /// <param name="profileTypeGroupId">Param profileTypeGroupId</param>
        /// <returns>Return ProfileTypeGroup</returns>
        public ProfileTypeGroupEN GetProfileTypeGroupById(int profileTypeGroupId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ProfileTypeGroupEN profileTypeGroup = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeGroupGetById, profileTypeGroupId);
                while (reader.Read())
                {
                    profileTypeGroup = new ProfileTypeGroupEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileTypeGroup.ProfileTypeGroupId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeGroup.Name = reader.GetString(1);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return profileTypeGroup;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all ProfileTypeGroup
        /// </summary>
        /// <returns>Return list all ProfileTypeGroup</returns>
        public IList<ProfileTypeGroupEN> GetProfileTypeGroups()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileTypeGroupEN> lstProfileTypeGroup = new List<ProfileTypeGroupEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeGroupGetAll);
                while (reader.Read())
                {
                    ProfileTypeGroupEN profileTypeGroup = new ProfileTypeGroupEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileTypeGroup.ProfileTypeGroupId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeGroup.Name = reader.GetString(1);
                    }

                    lstProfileTypeGroup.Add(profileTypeGroup);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileTypeGroup;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db ProfileTypeGroup
        /// </summary>
        /// <param name="profileTypeGroup">Paarm ProfileTypeGroup</param>
        /// <returns>Insert ProfileTypeGroup</returns>
        public int Insert(ProfileTypeGroupEN profileTypeGroup)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeGroup.Name, profileTypeGroup.Name)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeGroupInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db ProfileTypeGroup
        /// </summary>
        /// <param name="profileTypeGroup">Param ProfileTypeGroup</param>
        public void Update(ProfileTypeGroupEN profileTypeGroup)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeGroup.ProfileTypeGroupId, profileTypeGroup.ProfileTypeGroupId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeGroup.Name, profileTypeGroup.Name),
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeGroupUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete ProfileTypeGroup by profileTypeGroupId
        /// </summary>
        /// <param name="profileTypeGroupId">param profileTypeGroupId</param>
        public void Delete(int profileTypeGroupId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeGroup.ProfileTypeGroupId, profileTypeGroupId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeGroupDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exists the specified profile type group id.
        /// </summary>
        /// <param name="profileTypeGroupId">The profile type group id.</param>
        /// <param name="name">The name.</param>
        /// <returns>return result > 0</returns>
        /// <exception cref="System.Exception">SqlException ex</exception>
        public bool Exist(int profileTypeGroupId, string name)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeGroup.ProfileTypeGroupId, profileTypeGroupId),
                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeGroup.Name, name)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeGroupExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}